package com.woniuxy.joylifecenter.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.constant.RealNameCertificationConstant;
import com.woniuxy.common.constant.UserTypeConstant;
import com.woniuxy.common.entity.CenterUser;
import com.woniuxy.joylifecenter.feign.RealNameCertificationFeinService;
import com.woniuxy.joylifecenter.feign.RealNameCertificationFeinService;
import com.woniuxy.joylifecenter.service.UserManageService;
import com.woniuxy.joylifecenter.vo.AuthVO;
import com.woniuxy.joylifecenter.vo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.util.*;

@Api(value = "", description = "后台用户管理API")
@Controller
public class Center_User_Controller {
    @Autowired
    UserManageService userManageService;

    @Autowired
    RealNameCertificationFeinService realNameCertificationFeinService;

    @ResponseBody
    @RequestMapping("/realNameCertification")
    public VO certification(@RequestBody HashMap<String, String> verifyMap) {
        AuthVO authVO = new AuthVO();
        authVO.setCenterCridentialCode(verifyMap.get("center_credentialcode"));
        authVO.setCenterUsertype(new Byte(verifyMap.get("center_usertype")));
        authVO.setId(verifyMap.get("id"));
        authVO.setName(verifyMap.get("name"));
        authVO.setPhone(verifyMap.get("phone"));
        Integer verifyResult = realNameCertificationFeinService.authenticate(authVO);
        VO vo = new VO();
        vo.setData(null);
        vo.setCode(0);
        if (verifyResult != null) {
            vo.setCount(1);
        } else {
            vo.setCount(0);
        }

        if (verifyResult == RealNameCertificationConstant.PASS) {
            vo.setMsg(verifyMap.get("name") + "实名认证通过");
        } else if (verifyResult == RealNameCertificationConstant.REJECT) {
            vo.setMsg(verifyMap.get("name") + "实名认证不通过");
        }
        return vo;

    }

    @ApiOperation(value = "/showAllUserApply", notes = "查询所有待审批用户认证")
    @RequestMapping("/showAllUserApply")
    @ResponseBody
    public VO showAllUserApply() {
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("");
        int count = 0;
        List<Map<String, Object>> listmap = userManageService.queryAllUserApply();
        if (listmap != null) {
//            List<Object> list = new ArrayList<Object>();
//            list.add(map);
            vo.setCount(listmap.size());
            vo.setData(listmap);
        } else {
            vo.setCount(count);
            vo.setData(null);
        }
        return vo;
    }


    @ApiOperation(value = "/passUserApply", notes = "通过用户认证")
    @RequestMapping("/passUserApply")
    @ResponseBody
    public VO passUserApply(@RequestBody String id) {
        System.out.println("后端收到的参数:" + id);
        VO vo = new VO();
        vo.setCode(0);
        vo.setData(null);
        int count = 0;
        count = userManageService.passUserApply(id);
        if (count == 1) {
            vo.setMsg("已通过认证");
        } else {
            vo.setMsg("操作未成功");
        }

        return vo;
    }

    @ApiOperation(value = "/rejectUserApply", notes = "用户认证拒绝")
    @RequestMapping("/rejectUserApply")
    @ResponseBody
    public VO rejectUserApply(@RequestBody String id) {
        System.out.println("后端收到的参数:" + id);
        VO vo = new VO();
        vo.setCode(0);
        int count = 0;
        count = userManageService.rejectUserApply(id);
        vo.setData(null);
        if (count == 1) {
            vo.setMsg("已拒绝认证");
        } else {
            vo.setMsg("操作未成功");
        }
        return vo;
    }

    @ApiOperation(value = "/showUserByCondition", notes = "多条件查用户")
    @RequestMapping("/showUserByCondition")
    @ResponseBody
    public VO showUserByCondition(@RequestBody HashMap<String, String> josnsMap) throws ParseException, IOException {
        String jsonstr = josnsMap.get("jsonstr");
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> map = mapper.readValue(jsonstr, Map.class);
        System.out.println("后端收到的参数:" + map);
        CenterUser centerUser = new CenterUser();
        if ((map.get("date") != null) && !(map.get("date").equals(""))) {
            centerUser.setDate(DateFormat.getDateTimeInstance().parse(map.get("date")));
        }
        if ((map.get("centerCredentialcode") != null) && (!map.get("centerCredentialcode").equals(""))) {
            centerUser.setCenterCredentialcode(map.get("centerCredentialcode"));
        }

        if ((map.get("centerUsertype") != null) && (!map.get("centerUsertype").equals(""))) {
            centerUser.setCenterUsertype(new Byte(map.get("centerUsertype")));
        }
        if ((map.get("coin") != null) && (!map.get("coin").equals(""))) {
            centerUser.setCoin(new Integer(map.get("coin")));
        }

        centerUser.setEmail(map.get("email"));
        centerUser.setForumId(map.get("forumId"));
        centerUser.setId(map.get("id"));
        centerUser.setIntro(map.get("intro"));
        centerUser.setName(map.get("name"));
        centerUser.setNickname(map.get("nickname"));
        centerUser.setPhone(map.get("phone"));

        VO vo = new VO();
        vo.setCode(0);

        PageHelper.startPage(new Integer(josnsMap.get("page")), new Integer(josnsMap.get("limit")));
        List<CenterUser> centerUsers = userManageService.selectUserByCondition(centerUser);
        System.out.println("后端查到要返回给前端的数据:" + centerUsers);
        PageInfo<CenterUser> pageInfo = new PageInfo<>(centerUsers);
        List<PageInfo> pageInfos = new ArrayList<PageInfo>();
        pageInfos.add(pageInfo);
        vo.setCount(new Long(pageInfo.getTotal()).intValue());
        vo.setData(pageInfos);
        vo.setMsg("");
        System.out.println("后端VO:" + vo);
        return vo;
    }


    @ApiOperation(value = "/suspendAccount", notes = "封号")
    @RequestMapping("/suspendAccount")
    @ResponseBody
    public VO suspendAccount(@RequestBody String id) {
        System.out.println("后端收到的参数:" + id);
        VO vo = new VO();
        vo.setCode(0);
        int count = 0;
        count = userManageService.suspendAccount(id);
        vo.setData(null);
        if (count == 1) {
            vo.setMsg("已封号");
        } else {
            vo.setMsg("操作未成功");
        }
        return vo;
    }


    @ApiOperation(value = "/unsuspendAccount", notes = "账号解封")
    @RequestMapping("/unsuspendAccount")
    @ResponseBody
    public VO unsuspendAccount(@RequestBody String id) {
        System.out.println("后端收到的参数:" + id);
        VO vo = new VO();
        vo.setCode(0);
        int count = 0;
        count = userManageService.unsuspendAccount(id);
        vo.setData(null);
        if (count == 1) {
            vo.setMsg("已解封");
        } else {
            vo.setMsg("操作未成功");
        }
        return vo;
    }


    @ApiOperation(value = "/resetPassWord", notes = "重置密码")
    @RequestMapping("/resetPassWord")
    @ResponseBody
    public VO resetPassWord(@RequestBody String id) {
        System.out.println("后端收到的参数:" + id);
        VO vo = new VO();
        vo.setCode(0);
        int count = 0;
        count = userManageService.resetPassWord(id);
        vo.setData(null);
        if (count == 1) {
            vo.setMsg("密码已重置");
        } else {
            vo.setMsg("操作未成功");
        }
        return vo;
    }

}
